package com.java.day09.homework.test;

class Team {
    Player[] players;

    Team(Player[] players) {
        this.players = players;
    }

    boolean isTeamAlive() {
        for (Player player : players) {
            if (player.isAlive()) {
                return true;
            }
        }
        return false;
    }

    void attack(Team enemyTeam) {
        // 随机选择一个存活的玩家进行攻击
        Player attackingPlayer = null;
        for (Player player : players) {
            if (player.isAlive()) {
                attackingPlayer = player;
                break;
            }
        }

        if (attackingPlayer != null) {
            // 随机选择敌方一个存活的玩家进行攻击
            Player enemyPlayer = null;
            for (Player player : enemyTeam.players) {
                if (player.isAlive()) {
                    enemyPlayer = player;
                    break;
                }
            }

            if (enemyPlayer != null) {
                enemyPlayer.takeDamage(attackingPlayer.attackPower);
                System.out.println(attackingPlayer.name + " attacks " + enemyPlayer.name + " for " + attackingPlayer.attackPower + " damage!");
                if (!enemyPlayer.isAlive()) {
                    System.out.println(enemyPlayer.name + " has been defeated!");
                }
            }
        }
    }
}
